package com.presto.directo.mvvm.view

import android.content.Intent
import android.os.Bundle
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.widget.doOnTextChanged
import com.presto.directo.R
import com.presto.directo.base.BaseActivity
import com.presto.directo.databinding.ActivityLoginPhoneBinding
import com.presto.directo.event.LoginSuccess
import com.presto.directo.mvvm.viewmodel.*
import com.presto.directo.net.ApiService.Companion.URL_PRIVATE
import com.presto.directo.utils.*
import org.greenrobot.eventbus.*

class LoginPhoneActivity : BaseActivity() {

    private val binding by lazy { ActivityLoginPhoneBinding.inflate(layoutInflater) }
    private val viewModel: LoginViewModel by lazy { getViewModel() }

    override val rootView: View
        get() = binding.root

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Af.show_phone_login_page.track(this)
        loanStatus = emptyStatus()
        EventBus.getDefault().register(this)
        viewModel.smsLiveData.observe(this) {
            val intent = Intent(this, LoginSmsActivity::class.java)
            intent.putExtra("phone", it)
            startActivity(intent)
        }
        binding.tvTerm.text = SpannableStringBuilder().append("Estoy de acuerdo con los ")
            .append(setTextClick("Téminos y condiciones", ""))
        binding.tvTerm.movementMethod = LinkMovementMethod.getInstance()
        binding.tvTerm.highlightColor = ContextCompat.getColor(this, R.color.transparent)
        binding.tvPrivate.text = SpannableStringBuilder().append("Acepto el ")
            .append(setTextClick("Aviso de Privacidad", URL_PRIVATE))
        binding.tvPrivate.movementMethod = LinkMovementMethod.getInstance()
        binding.tvPrivate.highlightColor = ContextCompat.getColor(this, R.color.transparent)
    }

    override fun initEvent() {
        binding.btnGetCode.setOnClickListener {
            Af.click_get_verify_code.track(this)
            viewModel.getSmsCode(binding.etPhone.text.toString())
        }
        binding.etPhone.doOnTextChanged { text, start, before, count -> updateBtn() }
        binding.cbPrivate.setOnCheckedChangeListener { buttonView, isChecked -> updateBtn() }
        binding.cbTerm.setOnCheckedChangeListener { buttonView, isChecked -> updateBtn() }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun loginSuccess(loginSuccess: LoginSuccess) {
        finish()
    }

    override fun updateBtn() {
        binding.btnGetCode.isEnabled = binding.cbTerm.isChecked && binding.cbPrivate.isChecked && binding.etPhone.text.isNotBlank()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }

    override fun isTransparentStatusBar(): Boolean {
        return true
    }

    override fun isDarkStatusBar(): Boolean {
        return true
    }

}